/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 27.11.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import main.framework.http.Method;

/**
 * This class represents a resource object.
 */
public class Resource {

	private ResourceURI resourceURI;
	private Map<Method, ResourceMethod> resourceMethods;
	private List<Resource> childResources = new ArrayList<Resource>();
	
	/**
	 * Creates a resource.
	 * 
	 * @param resourceURI The URI of the resource.
	 */
	public Resource(ResourceURI resourceURI) {
		this.resourceURI = resourceURI;
		this.resourceMethods = new HashMap<>();
	}
	/**
	 * Creates a resource.
	 * 
	 * @param resourceURI The URI of the resource.
	 * @param resourceMethods The assigned and available resource methods.
	 */
	public Resource(ResourceURI resourceURI, Map<Method, ResourceMethod> resourceMethods) {
		this.resourceURI = resourceURI;
		this.resourceMethods = resourceMethods;
	}
	
	/**
	 * Gets the resource URI.
	 * 
	 * @return The URI of the resource.
	 */
	public ResourceURI getResourceURI() {
		return this.resourceURI;
	}
	
	/**
	 * Gets the last node of the resource URI.
	 * 
	 * @return The last node of the resource URI.
	 */
	public ResourceURINode getURINode() {
		return this.resourceURI.getLastNode();
	}
	
	/**
	 * Adds a child resource.
	 * 
	 * @param childResource The child resource to add.
	 */
	public void addChild(Resource childResource) {
		this.childResources.add(childResource);
	}
	
	/**
	 * Gets all children resources of the resource.
	 * 
	 * @return All children resources of the resource.
	 */
	public List<Resource> getChildren() {
		return this.childResources;
	}
	
	/**
	 * Gets the resource method which is assigned to the given HTTP method.
	 * 
	 * @param httpMethod The HTTP method whose associated resource method is in demand.
	 * @return The resource method which is assigned to the given HTTP method, or null if the resource method does not exist.
	 */
	public ResourceMethod getResourceMethod(Method httpMethod) {
		if(this.resourceMethods.containsKey(httpMethod)){
			return this.resourceMethods.get(httpMethod);
		}
		else{
			return null;
		}
	}
	
	/**
	 * Sets the resource method  for to the given HTTP method.
	 * 
	 * @param httpMethod The HTTP method for which a corresponding resource method is set.
	 * @param resourceMethod The resource method which should be set for the given HTTP method.
	 */
	public void setResourceMethod(Method httpMethod, ResourceMethod resourceMethod) {
		this.resourceMethods.put(httpMethod, resourceMethod);
	}
	
}
